package day01;

import java.util.HashMap;
import java.util.Map;

public class NullTest {
    public static void main(String[] args) {
        Map<String,Boolean> map =  new HashMap<>();//map的value为包装类型，在下面的三目运算时会自动拆箱成基本类型
        boolean b = (map!=null ? map.get("test"):false);//map的size为0，但是map不为null,得到map.get("test")=null,null赋值给基本变量，报空指针
        System.out.println(b);
        /**
        *   map.get("test")=null,用Boolean 来接就不会报空指针，得到null
         *   这是因为三目运算符的语法规范
         *   If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
         *  如果第二个和第三个操作数具有相同的类型(可能是null类型)，这就是条件表达式的类型
         *   If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.
         *  如果第二个和第三个操作数中有一个是基本类型T，另一个的类型是T对应的包装类型，则条件表达式的类型为T(运算时自动对包装类型T拆箱)
         *  If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
         *  如果第二个和第三个操作数中有一个为null类型，另一个操作数的类型为引用类型，则条件表达式的类型为该引用类型(null可以转成任何类型)。
        */
//JDK1.8以后修复了，用Boolean接收不会报空指针，用boolean接收会报空指针
    }
}
